<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use think\facade\Log;


class Order extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('app\command\order')
            ->setDescription('the app\command\order command');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $order=\app\model\Order::where('status',1)
            ->where('created_at','<',time()-60)
            ->with('detail.product')
            ->select();

        try {
            Db::startTrans();

            foreach ($order as $item){
                $order->status=60;
                $order->save();

                foreach ($item->detail as $details){
                    $details->product->inc('num',$details->num)->update();
                }

            }
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            Log::error($e);
        }

    }
}
